﻿using AntMovement.Core;
using AntMovement.Core.Attributes;
using AntMovement.Core.Entity;
using AntMovement.Core.Hardware;
using AntMovement.Core.IParameter;
using AntMovement.Core.Logic;
using AntMovement.Core.Logic.Base;
using AntMovement.Core.Model;
using AntMovement.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.Handle.TPTest
{
    [FlowAttributes(nameof(LoadMaterial), 3)]
    public class LoadMaterial : TaskBase
    {
        #region 轴变量
        #endregion

        #region 上升沿
        #endregion

        #region 定时器
        private Ton _tonMaterialInspection = new Ton();
        #endregion

        #region 点位
        #endregion

        #region 输入点
        private NormalOpen _进料有料信号;
        private NormalOpen _进料横移气缸原点;
        private NormalOpen _进料横移气缸动点;
        private NormalOpen _进料升降气缸原点;
        private NormalOpen _进料升降气缸动点;
        private NormalOpen _进料吸嘴真空信号;
        private NormalOpen _缓存位真空信号;
        #endregion

        #region 输出点
        private ICoil _进料横移气缸;
        private ICoil _进料升降气缸;
        private ICoil _进料电机;
        private ICoil _进料吸嘴真空;
        private ICoil _进料吸嘴吹气;
        private ICoil _缓存位真空;
        #endregion

        #region 报警变量
        private bool _noMaterialDetected = false;
        #endregion

        #region 私有变量
        #endregion
        public LoadMaterial(ITemporaryParameter temporary, IIOParameter iop, IAxisParameter axisp, IDialogHost dialogHost, ILog log)
            : base(temporary, iop, axisp, dialogHost, log)
        {
            _进料有料信号 = iop.GetNormalOpen("进料有料信号");
            _进料横移气缸原点 = iop.GetNormalOpen("进料横移气缸原点");
            _进料横移气缸动点 = iop.GetNormalOpen("进料横移气缸动点");
            _进料升降气缸原点 = iop.GetNormalOpen("进料升降气缸原点");
            _进料升降气缸动点 = iop.GetNormalOpen("进料升降气缸动点");
            _进料吸嘴真空信号 = iop.GetNormalOpen("进料吸嘴真空信号");
            _缓存位真空信号 = iop.GetNormalOpen("缓存位真空信号");

            _进料横移气缸 = iop.GetCoil("进料横移气缸");
            _进料升降气缸 = iop.GetCoil("进料升降气缸");
            _进料电机 = iop.GetCoil("进料电机");
            _进料吸嘴真空 = iop.GetCoil("进料吸嘴真空");
            _进料吸嘴吹气 = iop.GetCoil("进料吸嘴吹气");
            _缓存位真空 = iop.GetCoil("缓存位真空");
        }

        public override void EmergencyStop()
        {

        }

        public override void Handle()
        {
            if (_temporary.SystemStatus.AlarmStop || _temporary.SystemStatus.ManualOperation)
            {
                _进料电机.Execute(false);
            }
            if (_temporary.SystemStatus.Auto)
            {
                _进料电机.Execute(!_进料有料信号.Execute());
            }
            if (SignalOn(0))
            {
                if (_进料有料信号.Execute())
                {
                    NextStep();
                }
            }
            if (SignalOn(10)) 
            {
                if (_进料横移气缸原点.Execute())
                {
                    _进料升降气缸.Execute(true);
                    NextStep();
                }
                else
                {

                }
            }
            if (SignalOn(20))
            {
                if (_进料升降气缸动点.Execute())
                {
                    _进料吸嘴吹气.Execute(false);
                    _进料吸嘴真空.Execute(true);
                    NextStep();
                }
            }
            if (SignalOn(30))
            {
                _tonMaterialInspection.Time(400, !_进料吸嘴真空信号.Execute());
                if (_进料吸嘴真空信号.Execute())
                {
                    _进料升降气缸.Execute(false);
                    NextStep();
                }
                else
                {
                    //报警
                    if (_tonMaterialInspection.Out)
                    {
                        _dialogHost.DialogShow($"[{_step}] 进料吸嘴未检测到物料", Core.Enum.LogLevel.Error, hostDialog: Core.Enum.PopUpType.Global);
                        _log.Warning($"[{_step}] 进料吸嘴未检测到物料!!!");
                        _noMaterialDetected = true;
                        _tonMaterialInspection.Time(100, false);
                    }
                }
            }
            if (SignalOn(40))
            {
                if (_进料升降气缸原点.Execute())
                {
                    _tonMaterialInspection.Time(400, !_进料吸嘴真空信号.Execute());
                    if (_进料吸嘴真空信号.Execute())
                    {
                        if (_temporary.Status_缓存工位.Permit_Blowing)
                        { 
                            _进料横移气缸.Execute(true);
                            NextStep();                        
                        }
                    }
                    else
                    {
                        //报警
                        if (_tonMaterialInspection.Out)
                        {
                            _dialogHost.DialogShow($"[{_step}] 进料吸嘴未检测到物料", Core.Enum.LogLevel.Error, hostDialog: Core.Enum.PopUpType.Global);
                            _log.Warning($"[{_step}] 进料吸嘴未检测到物料!!!");
                            _noMaterialDetected = true;
                            _tonMaterialInspection.Time(100, false);
                        }
                    }
                }
            }
            if (SignalOn(50))
            {
                if (_进料横移气缸动点.Execute())
                {
                    _进料升降气缸.Execute(true);
                    NextStep();
                }
            }
            if (SignalOn(60))
            {
                if (_进料升降气缸动点.Execute())
                {
                    _缓存位真空.Execute(true);
                    NextStep();
                }
            }
            if (SignalOn(70))
            {
                if (_缓存位真空信号.Execute())
                {
                    _进料吸嘴吹气.Execute(true);
                    _进料吸嘴真空.Execute(false);
                    _进料升降气缸.Execute(false);
                    NextStep();
                }
            }
            if (SignalOn(80))
            {
                if (_进料升降气缸原点.Execute())
                {
                    _进料吸嘴吹气.Execute(false);
                    _进料横移气缸.Execute(false);
                    NextStep();
                }
            }
            if (SignalOn(90))
            {
                if (_进料横移气缸原点.Execute())
                {
                    _step = 0;
                    _temporary.Status_缓存工位.DischargeComplete = true;
                }
            }
            if (_temporary.SystemStatus.ManualOperation)
            {

            }
            _temporary.SystemStatus.AlarmStop = _noMaterialDetected || _temporary.SystemStatus.AlarmStop;
        }

        public override void Resetting()
        {
            _noMaterialDetected = false;
        }

        public override void SafeStop()
        {

        }
    }
}
